package org.codeart.order.controller;

import org.codeart.common.dto.OrderInfo;
import org.codeart.common.request.OrderRequest;
import org.codeart.common.util.R;
import org.codeart.order.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping("/add")
    public R<Void> add(@RequestBody OrderRequest orderRequest, @RequestHeader("deviceCode") String deviceCode) {
        orderRequest.setDeviceCode(deviceCode);
        boolean b = orderInfoService.add(orderRequest);
        if (b) {
            return R.ok();
        }
        return R.fail("下单异常");
    }

    @PostMapping("/to-pick-up-passenger")
    public R<Void> toPickUpPassenger(@RequestBody OrderRequest orderRequest) {
        int i = orderInfoService.toPickUpPassenger(orderRequest);
        if (i != -1) {
            return R.ok();
        }
        return R.fail("更新订单失败");
    }

    /**
     * 到达乘客上车点
     */
    @PostMapping("/arrived-departure")
    public R<Void> arriveDeparture(@RequestBody OrderRequest orderRequest) {
        int i = orderInfoService.arriveDeparture(orderRequest);
        if (i != -1) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 司机接到乘客
     */
    @PostMapping("/pick-up-passenger")
    public R<Void> pickUpPassenger(@RequestBody OrderRequest orderRequest) {
        int i = orderInfoService.pickUpPassenger(orderRequest);
        if (i != -1) {
            return R.ok();
        }
        return R.fail("订单异常");
    }

    /**
     * 乘客到达目的地，行程终止
     */
    @PostMapping("/passenger-get-off")
    public R<Void> passengerGetOff(@RequestBody OrderRequest orderRequest) {
        int i = orderInfoService.passengerGetOff(orderRequest);
        if (i != -1) {
            return R.ok();
        }
        return R.fail("订单异常");
    }

    @PostMapping("/pay")
    public R<Void> pay(@RequestBody OrderRequest orderRequest) {
        boolean b = orderInfoService.pay(orderRequest);
        if (b) {
            return R.ok();
        }
        return R.fail("支付异常");
    }

    @PostMapping("/cancel")
    public R<Void> cancel(@RequestParam("orderId") Long orderId, @RequestParam("identity") String identity) {
        boolean b = orderInfoService.cancel(orderId, identity);
        if (b) {
            return R.ok();
        }
        return R.fail("订单取消失败");
    }

    /**
     * 订单详情
     * @param orderId 订单id
     */
    @GetMapping("/detail")
    public R<OrderInfo> detail(@RequestParam("orderId") Long orderId) {
        return orderInfoService.detail(orderId);
    }

    /**
     * 司机发起收款
     */
    @PostMapping("/push-pay-info")
    public R<Void> pushPayInfo(@RequestBody OrderRequest orderRequest) {
        return orderInfoService.pushPayInfo(orderRequest);
    }

    @GetMapping("/current")
    public R<OrderInfo> current(@RequestParam("phone") String phone, @RequestParam("identity") String identity) {
        return orderInfoService.current(phone, identity);
    }
}
